package com.SoftwareManage.service.impl;


import com.SoftwareManage.dao.UserDao;
import com.SoftwareManage.entity.User;
import com.SoftwareManage.entity.UserRole;
import com.SoftwareManage.service.UserService;
import com.SoftwareManage.shiro.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public Set<String> findPermByUsername(String username) {
        return userDao.findPermByUsername(username);
    }

    @Override
    public Set<String> getRolesByUsername(String username) {
        return userDao.getRolesByUsername(username);
    }

    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public List<UserRole> selectAlls(int page, int pageSize) {
        return userDao.selectAlls(page, pageSize);
    }

    @Override
    public List<User> search(String username) {
        return userDao.search(username);
    }

    @Override
    public List<UserRole> searchs(String username, int page, int pageSize) {
        return userDao.searchs(username, page, pageSize);
    }

    @Override
    public void update(int id, String username, String tel, String status, String remark,int role_id) {
        //更新用户表的用户信息
        userDao.update(id, username, tel, status, remark);
        //更新用户-角色表中的用户角色
        userDao.updateUserRole(id,role_id);
    }

    @Override
    public void add(String username, String password, String tel,String status, String remark,int role_id) {
        //1.生成随机盐
        String salt= SaltUtils.getSalt(8);
        //2.对明文密码进行MD5+salt+hash散列(2014为散列次数)
        Md5Hash md5Hash=new Md5Hash(password,salt,1024);
        //用户表里添加用户信息
        userDao.add(username, md5Hash.toHex(),salt,tel, status, remark);
        //根据用户名查找用户id
        int user_id=userDao.selectIdByUsername(username);
        //用户—角色表里设置用户角色
        userDao.setRole(role_id,user_id);
    }

    @Override
    public void delete(int id) {
        //删除用户-角色表中的关系
        userDao.deleteUserRole(id);
        //删除用户表中的用户
        userDao.delete(id);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public List<String> selectAllUsernames() {
        return userDao.selectAllUsernames();
    }

    @Override
    public void resetPassword(int id, String password) {
        //1.生成随机盐
        String salt= SaltUtils.getSalt(8);
        //2.对明文密码进行MD5+salt+hash散列(2014为散列次数)
        Md5Hash md5Hash=new Md5Hash(password,salt,1024);
        userDao.resetPassword(id, md5Hash.toHex(),salt);
    }

    @Override
    public String selectRoleName(int id) {
        return userDao.selectRoleName(id);
    }
}
